<?php

class Merchant_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function get_all($where = array(), $limit = 1, $offset = 0)
    {

        $rows = $this->get_merchants($where, $limit, $offset);
        $count = $this->db->query("SELECT FOUND_ROWS() as count")->row()->count;

        return array('rows' => $rows, 'count' => $count);
    }

    function get_merchants($where = array(), $limit = 1, $offset = 0)
    {
        $this->db->select('SQL_CALC_FOUND_ROWS *', false);
        $this->db->where($where);
        $this->db->limit($limit, $offset);
        $this->db->order_by('id', 'DESC');
        return $this->db->get('merchants')->result_array();
    }

    function get_by_id( $id = 0 )
    {
        $this->db->where('id', $id);
        return $this->db->get('merchants')->row_array();
    }

    function get_by_merchant_id( $mid = '' )
    {
        $this->db->where('merchant_id', $mid);
        return $this->db->get('merchants')->row_array();
    }

    function insert_key( $mid = '' )
    {
        return $this->db->insert('keys', array('key' => $mid));
    }

    function get_merchant_id_list()
    {
    	$rows = $this->db->get('merchants')->result_array();
    	
    	$merchnats = array();
    	foreach($rows as $row)
    	{
    		$merchnats[] = $row['merchant_id'];
    	}
    	
    	return $merchnats;
    }

}